package com.itheima.reggie_auto.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie_auto.common.R;
import com.itheima.reggie_auto.entity.Employee;
import com.itheima.reggie_auto.entity.RePassVO;
import com.itheima.reggie_auto.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author 刘少宽
 * @since 2022-09-23
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 用户登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request,@RequestBody Employee employee){

        //1.对密码MD5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.数据库根据用户名查询对象
        Employee emp = employeeService.getOne(employee.getUsername());
        //3.判断用户是否存在
        if (emp == null) {
            //用户名不存在
            return R.error("用户名不存在");
        }
        //4.判断密码是否正确
        if (!emp.getPassword().equals(password)) {
            //密码错误
            return R.error("密码错误");
        }
        //5.判断是否禁用
        if (emp.getStatus() == 0) {
            //用户已禁用
            return R.error("用户已被禁用，请联系管理员");
        }
        //6.将用户ID存储session
        request.getSession().removeAttribute("user");
        request.getSession().setAttribute("employee",emp.getId());

        return R.success(emp);
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        log.info("员工退出");
        //1.清空session
        request.getSession().invalidate(); //销毁session空间
        // request.getSession().removeAttribute("employee"); //只清空自己存的employee

        return R.success("退出成功");
    }


    /**
     * username校验
     * @param username
     * @return
     */
    @GetMapping("/checkUsername")
    public R<String> checkUsername(String username) {
        log.info("账号失去焦点校验：{}",username);
        Employee employee = employeeService.getOne(username);
        if (employee != null) {
            //用户名已存在
            return R.error("用户名已存在");
        }
        return R.success("用户名可以使用");
    }

    /**
     * 新增员工
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save( @RequestBody Employee employee){
        log.info("新增员工：{}",employee);

        //设置创建时间 创建人 更新时间  更新人
        // employee.setCreateTime(LocalDateTime.now());
        // employee.setUpdateTime(LocalDateTime.now());
        //通过session中获取创建用户ID
        // Long empId = (Long) session.getAttribute("employee");
        //设置用户Id
        // employee.setCreateUser(empId);
        // employee.setUpdateUser(empId);

        employeeService.save(employee);

        return R.success("新增员工成功");
    }

    /**
     * 分页带条件查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {

        log.info("接收到前端参数page = {},pageSize = {}, name = {}",page,pageSize,name);
        Page pageInfo = employeeService.selectByPage(page,pageSize,name);

        return R.success(pageInfo);
    }

    /**
     * 修改员工数据
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update( @RequestBody Employee employee) {
        log.info("修改员工数据：{}",employee);
        //处理数据 -- 填充更新人以及更新时间
        // employee.setUpdateTime(LocalDateTime.now());
        // employee.setUpdateUser((Long) session.getAttribute("employee"));

        employeeService.update(employee);
        return R.success("修改员工成功");
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            //有值
            return R.success(employee);
        }
        return R.error("查询员工信息失败");
    }

    @PutMapping("/repass")
    public R<String> updatePwd(HttpSession session,@RequestBody RePassVO rePassVO){
        Long empId = (Long) session.getAttribute("employee");
        employeeService.updatePwd(empId,rePassVO);
        return R.success("修改成功");
    }

}

